import router from "./index";
import store from "../store"; // eslint-disable-line
import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({ showSpinner: false });
// 路由白名单 不需要token可直接访问
const whiteList = ["/login"];
router.beforeEach((to, from, next) => {
  NProgress.start();
  const token = store.state.user.token;
  if (token) {
    if (!router.options.DynamicMenuRoutes) {
      router.options.DynamicMenuRoutes = true;
      store.dispatch("permission/GenerateRoutes").then(() => {
        next({ ...to, replace: true });
        // next();
      });
      // permissionStore.GenerateRoutes();
      // next();
    } else {
      if (to.path === "/login") {
        next("/");
      } else {
        next();
      }
    }
  } else {
    // 没有token
    if (whiteList.includes(to.path)) {
      // 在免登录白名单，直接进入
      next();
    } else {
      next(`/login?redirect=${to.fullPath}`); // 否则全部重定向到登录页
    }
  }
  // 如果是需要登录的页面并且已经登陆了
  next();
});
/* eslint-disable */
router.afterEach((to, from) => {
  NProgress.done();
  // console.log(to, from)
  // 关闭加载提示
  // store.dispatch('changeLoad', false)
});
